package com.zx.eval.api.front;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.common.utils.R;
import com.zx.eval.entity.*;
import com.zx.eval.service.QuestionService;
import com.zx.eval.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.*;

@Controller("frontQuestionApi")
@RestController
@RequestMapping("/front/question")
public class QuestionApi {
    /**
     * 测评列表
     */
    @RequestMapping("/questionList")
    public R questionList(Integer id) {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", id);
        queryWrapper.orderByAsc("question_id");
        List<QuestionEntity> list = questionService.list(queryWrapper);
        return R.ok().put("info", list);
    }

    @RequestMapping("/checkUser")
    public R checkUser(String memberName, String tel) {
        QueryWrapper<ResultEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", tel);
        queryWrapper.eq("member_name", memberName);
        List<ResultEntity> list = resultService.list(queryWrapper);

        return R.ok().put("info", list);
    }

    /**
     * 测评问题列表
     */
    @RequestMapping(value = "/saveResult",method = RequestMethod.POST)
    public R saveResult(@RequestBody ResultEntity result ) {
        JSONArray selectedArr = JSONObject.parseArray(result.getAnswer());
        result.setEndTime(new Date());
        result.setOptime(new Date());

        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", 1);
        queryWrapper.orderByAsc("question_id");
        List<QuestionEntity> questionList = questionService.list(queryWrapper);
        int yesCount = 0;
        int noCount = 0;

        if(selectedArr != null && selectedArr.size()>0){
            for (int i = 0; i < selectedArr.size(); i++) {
                JSONArray answerArr = (JSONArray)(selectedArr.get(i));
                System.out.println(answerArr);
                // 取正常答案
                String rightAnswter = questionList.get(i).getAnswer();

                if ( answerArr != null && answerArr.size()>0){
                    String currAnswer = "";
                    for (int j=0 ; j< answerArr.size(); j++){
                        Integer a = answerArr.getInteger(j);
                        currAnswer = currAnswer + a;
                    }
                    // 比对答案
                    if (currAnswer.equals(rightAnswter)){
                        yesCount ++;
                    }else{
                        noCount ++ ;
                    }
                }
         }
        }
        result.setResultType(yesCount+"");
        resultService.save(result);
        return R.ok().put("yesCount", yesCount+"").put("noCount", noCount);

}

    @Autowired
    private QuestionService questionService;
    @Autowired
    private ResultService resultService;
}
